<template>
	<view class="uni-common-mt">
		<view class="uni-form-item uni-column" style="width: 80%; margin: 0 auto;">
			<view class="title">用户名</view>
			<input class="uni-input" style="border: 1px solid black;" placeholder="输入用户名" v-model="user_name"/>
		</view>
		<view class="uni-form-item uni-column" style="width: 80%; margin: 0 auto;">
			<view class="title">密码</view>
			<input class="uni-input"  placeholder="输入密码" password style="border: 1px solid black;" v-model="password"/>
		</view>
		<view class="a">
			<button @tap="login">登录</button>
			<!-- <button @tap="register">注册</button> -->
		</view>
		<popview4 ref="popview4"></popview4>
		<loading
			ref="loading"
			:custom="false"
			:shadeClick="true"
			:type="1"
			>
			<view style="text-align: center;">等待中</view>
		</loading>
		<uni-popup ref="popup" type="center" background-color="#fff">
			<view class=""  style="background-color: #fff; width: 700rpx;height: 400rpx;">
			
			<h1 style="text-align: center; font-size: 20px;margin-top: 10%;">请选择登录身份</h1>

			</view>

			<button type="primary" style="background: #4e72d5;width: 90%;" @tap="checkType('people')">普通用户</button>
			<button type="primary" style="background: #ff0000; margin-top: 5%;margin-bottom: 5%;width: 90%;" @tap="checkType('leader')">管理员</button>
		</uni-popup>
		<uni-popup ref="popup2" type="center" background-color="#fff">
			<view class=""  style="background-color: #fff; width: 700rpx;height: 400rpx;">
			
			<h1 style="text-align: center; font-size: 20px;margin-top: 10%;">输入用户名</h1>

			</view>

			<uni-easyinput v-model="nick_name_wx" placeholder="输入用户名"></uni-easyinput>
			<button type="primary" style="background: #00aaff; margin-top: 5%;margin-bottom: 5%;width: 90%;" @tap="registerWx">注册</button>
		</uni-popup>
	</view>
</template>

<script>
	import popview4 from '@/components/pop-view/pop-view4.vue'
	import loading from '../../components/xuan-loading/xuan-loading.vue'
	export default {
		components: {
			popview4,
			loading
		  },
		data() {
			return {
				user_name:"",
				user_name_wx:"",
				nick_name_wx:"",
				password:"",
				type:""
			}
		},
		onReady() {
			this.$refs.popup.open('top')
		},

		onLoad() {
			let value = uni.getStorageSync('user_info');
			if(value){
				uni.reLaunch({
					url:"/pages/index/index"
				})
			}
			
		},
		methods: {
			register(){
				uni.navigateTo({
					url:"/pages/regiser/regiser"
				})
			},
			registerWx(){
				uniCloud.callFunction({
					name: 'wxRegister',
					data: {
						"user_name":this.user_name_wx,
						"nick_name":this.nick_name_wx,
						"password":"",
						"type":"people"
					}
				  })
				  .then(res => {
						this.$refs.loading.open();
						uniCloud.callFunction({
							name: 'login',
							data: {
								"user_name":this.user_name_wx,
								"password":"",
								"type":"people"
							}
						  })
						  .then(res => {
							  let result=res.result
							  console.log(result)

							  uni.setStorageSync('user_info', result.user_info);
							  this.$refs.loading.close();
							  this.$refs.popup2.close()
							  uni.reLaunch({
								 url:"/pages/index/index"
							  })
							  
						  });
					
				  });			
			},
			l2(){
				var that=this
				this.$refs.loading.open();
			uni.login({
			  'provider': 'weixin',
			  success:(res)=>{
				  console.log(res.code)
			  uniCloud.callFunction({
			  	name:"wxLogin",
				data:{
					"code":res.code
				}
				})
			  .then(res => {
				  console.log(res)
				  this.user_name_wx=res.result.openid
				if(res.result.status==2){
					//注册后再登录
					this.$refs.loading.close();
					this.$refs.popup2.open('top')
				}else{
					//直接登录
						uniCloud.callFunction({
							name: 'login',
							data: {
								"user_name":this.user_name_wx,
								"password":"",
								"type":"people"
							}
						  })
						  .then(res => {
							  let result=res.result
							  console.log(result)

							  uni.setStorageSync('user_info', result.user_info);
							  this.$refs.loading.close();
							  uni.reLaunch({
								 url:"/pages/index/index"
							  })
							  
						  });
				}
				//that.$refs.loading.close();
			  });
				// uni.request({
				// 	url: 'https://api.weixin.qq.com/sns/jscode2session',
				// 	data: {
				// 		'appid': 'wxc7fe3adadeaa9875',
				// 		'secret': '1f18e26bdfef693477ac23b3c24efb54',
				// 		'js_code': code, // wx.login登录code
				// 		'grant_type': 'authorization_code' // 固定赋值
				// 	},
				// 	success(res) {
				// 		console.log('res', res)
				// 		// _this.openid = res.data.openid 
				// 	}
				// })
				// 获取用户信息
				// uni.getUserInfo({
				//   provider: 'weixin',
				//   success: function (infoRes) {
				// 	console.log(infoRes);
				//   }
				// });
			  }
			});
			},
			checkType(type){
				if(type=='leader'){
					 this.$refs.popup.close()
				}else{
					this.l2()
				}
			},
			login(){
				if(this.user_name.length==0){
					this.$refs['popview4'].error('请输入用户名');
				}else if(this.password.length==0){
					this.$refs['popview4'].error('请输入密码');
				}else{
					this.$refs.loading.open();
					uniCloud.callFunction({
					    name: 'login',
					    data: {
							"user_name":this.user_name,
							"password":this.password,
							"type":"leader"
						}
					  })
					  .then(res => {
						  let result=res.result
						  console.log(result)
						  if(result.status==2){
							  uni.setStorageSync('user_info', result.user_info);
							  this.$refs.loading.close();
							uni.reLaunch({
								url:"/pages/index/index"
							})
						  }else if(result.status==0){
							  this.$refs.loading.close();
							  this.$refs['popview4'].error('用户名不存在');
						  }else if(result.status==-1){
							 this.$refs.loading.close();
							 this.$refs['popview4'].error('密码错误'); 
						  }
						
					  });
				}
			}
		}
	}
</script>

<style scoped>
	.a{
		display: flex;
		margin-top: 5%;
	}
</style>
